const app = getApp();
const publicFun = require('/utils/public.js');
import { userFeedBackList,getriskContent, userFeedBack} from '/utils/request'
Page({
  data: {
    feedbackList: [],
    pageindex: 1,

    userName: '写下你想说的...',
    feedbackComment: '', //全部评论单个内容
    openComment: false, //评论弹框
  },
  onLoad() {
    this.setData({
      feedbackList: [],
      userData: app.globalData.userData,
      systemInfo: app.globalData.systemInfo
    })
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    this.feedBackList(this.data.pageindex);
  },
  closeKeyboard(e) {
    this.setData({
      openComment: false,
      feedbackComment: ''
    })
  },
  getCommentVal(e) { //获取评论框里的内容
    this.setData({
      feedbackComment: e.detail.value
    })
  },
  onReplyFeedback(e) { //回复反馈
    let data = e.currentTarget.dataset;
    if(data.risUser == 2){ //客服
      this.setData({
        openComment: true,
        feedbackId: data.id
      })
    }
  },
  async commentListSub() {
    let that = this;
    try {
      if(this.data.feedbackComment){
        let params = {
          content: this.data.feedbackComment,
        }
        const res = await getriskContent(params)
        if(res.S == 1){
          if(res.D.action == 'PASSED'){
            my.showLoading();
            that.submitComment();
          }else{
            setTimeout(function () {
              publicFun.showToast("您输入的内容涉及敏感词汇，请重新输入。", 2000);
              that.setData({
                feedbackComment: ''
              });
            }, 300)
          }
        }else{
          publicFun.showToast(res.M ? res.M : '加载失败');
        }
      }else{
        publicFun.showToast("请输入评论内容")
      }
    } catch (e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },
  async submitComment() {
    try {
      let VersionInfo = this.data.systemInfo.brand+'-'+this.data.systemInfo.model+'-'+this.data.systemInfo.system;
      var params = {
        UserId: app.globalData.userid,
        Message: this.data.feedbackComment,
        IOSOrAndroid: '支付宝气候行动小程序', // ios或者安卓
        System: this.data.systemInfo.platform, // 系统-系统名字
        Version: VersionInfo, // 版本
        AppVersion: this.data.systemInfo.version, //APP版本-支付宝版本
        Location: app.globalData.location.lng+','+app.globalData.location.lat,  //定位
        Pics: '', //图片
        Contactmethord: '', 
        ParentId: this.data.feedbackId, //如果是提交反馈，本参数传0，如果是回复某反馈，传被反馈id
      }
      const res = await userFeedBack(params)
      my.hideLoading();
      if (res.S == 1) {
        publicFun.showToast("提交成功", 2000);
        this.setData({
          feedbackComment: '',
          userName: '写下你想说的...',
          openComment: false,
        });
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    } catch (e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },

  async feedBackList(pageindex) {
    let that = this;
    try {
      let params = {
        UserId: app.globalData.userid ? app.globalData.userid : 0,
        PageIndex: pageindex,
        Id: 0,
      }
      my.showLoading();
      const res = await userFeedBackList(params);
      my.hideLoading();
      if (res.S == 1) {
        if(res.L.length > 0){
          res.L.forEach((item) => {   
            item.De = publicFun.Base64.decode(item.De);
            if(item.HF.length > 0){
              item.HF.forEach((cItem) => {
                cItem.RDe = publicFun.Base64.decode(cItem.RDe);
              })
            }
            that.data.feedbackList.push(item);
          });
        }else{
          publicFun.showToast("已经加载到底啦")
        }
        that.setData({
          feedbackSum: res.C,
          feedbackList: that.data.feedbackList
        })
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    } catch (e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  previewImage(e) { //查看大图
    let data = e.currentTarget.dataset;
    let arr = [];
    for(let i=0;i<data.list.length;i++){
      arr.push(data.list[i].Url.replace(/\/ico/g, ''))
    }
    my.previewImage({
      current: data.index,
      enablesavephoto: true,
      urls: arr
    });
  },
  onReachBottom() {
    const newPage = this.data.pageindex + 1;
    this.feedBackList(newPage);
    this.setData({pageindex: newPage});
  }
});
